# coding: utf-8
from flask import render_template
from flask import request, session
from flask import Blueprint
from tools import logging, wrapsfuncs
from dbModels import AgmStaffRepair, AgmStaffReport, AgmStaffReview, AgmStaffUser
from leancloud import Query, Object
from agmbeta import printDateTime

app = Blueprint('agmstaff', __name__)


def getStaffInfo(openid):
    query = Query(AgmStaffUser)
    user = query.equal_to('openid', openid).find()
    if user:
        return user[0]
    
fixer = {
    "ohCrcvlQMzCovAxALwzdNOs8yNf8": "处理人",
    "ohCrcvngXDgu-CulbG2S2IDXYbt4": "刘璐",
    "ohCrcvtsx1KzJ0L1rYMGBUzTHT_s": "吴晓雯"
    }    
fixer_notify = [
    'ohCrcvlQMzCovAxALwzdNOs8yNf8', 
    'ohCrcvngXDgu-CulbG2S2IDXYbt4', 
    'ohCrcvtsx1KzJ0L1rYMGBUzTHT_s'
    ]
admins = {
    "ohCrcvlQMzCovAxALwzdNOs8yNf8": "管理员",
    "ohCrcvngXDgu-CulbG2S2IDXYbt4": "刘璐",
    "ohCrcvpGGtIBJTNo5LUpR2dfb-Us": "英博",
    "ohCrcvqJnQdBw1kAgkgzkFgP5ROQ": "英达"
    }
admins_notify = [
    'ohCrcvlQMzCovAxALwzdNOs8yNf8', 
    'ohCrcvngXDgu-CulbG2S2IDXYbt4', 
    'ohCrcvpGGtIBJTNo5LUpR2dfb-Us', 
    ]
places = [u'宿舍', u'工厂']
floors = [u'一楼', u'二楼', u'三楼', u'四楼']

@app.route('/')
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def index():
    return render_template('agmstaff/index.html')

@app.route('/report', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def report():
    openid = session.get('openid')
    #openid = session.get('openid') or 'ohCrcvlQMzCovAxALwzdNOs8yNf8'
    agmstaffuser = Query(AgmStaffUser).equal_to('openid', openid).find()
    if not agmstaffuser:
        return render_template('agm1.0/agm_wx_deny.html')
    if request.method == 'POST':
        phone = request.form.get('phone')
        report_type = request.form.get('report_type')
        des = request.form.get('des')
        report = AgmStaffReport(phone=phone, des=des, report_type=report_type, mode=1, state=0)
        report.set('name', agmstaffuser[0].get('name'))
        report.set('depart', agmstaffuser[0].get('depart'))
        report.set('openid', openid)
        report.save()
        from tools.wechatAPI import send_report_message
        r_mode = [u'投诉', 'reports'] 
        for admin in admins_notify:
            send_report_message(admin, r_mode, report.get('name'), report.get('depart'), phone, report_type, des)
        r_code = "%s%s" % (printDateTime(report.created_at, '%Y%m%d%H%M%S'),
    report.id[-3:])
        r_code = [r_code, 'reports']
        return render_template('agmstaff/msg.html', keyword=u"投诉", r_code=r_code)
    name = agmstaffuser[0].get('name')
    return render_template('agmstaff/report.html', keyword=u"投诉", name=name)

@app.route('/reports', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def reports():
    openid = session.get('openid')
    #openid = session.get('openid') or 'ohCrcvlQMzCovAxALwzdNOs8yNf8'
    #openid = session.get('openid') or 'ohCrcvngXDgu-CulbG2S2IDXYbt4'
    agmstaffuser = Query(AgmStaffUser).equal_to('openid', openid).find()
    if not agmstaffuser and openid not in admins:
        return render_template('agm1.0/agm_wx_deny.html')
    if request.method == 'POST':
        if openid in admins:
            state = int(request.args.get('state'))
            rid = request.args.get('rid')
            report = Query(AgmStaffReport).get(rid)
            report.set('state', state+1)
            report.set('dealName', admins[openid]).save()
            return 'success'
    reports = Query(AgmStaffReport).equal_to('mode', 1).descending('createdAt').find()
    isadmin = openid in admins
    return render_template(
        'agmstaff/reports.html', 
        reports=reports, 
        int=int, 
        keyword=u"投诉",
        link="report", 
        isadmin=isadmin, 
        printDateTime=printDateTime)
    
@app.route('/suggests', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def suggests():
    openid = session.get('openid')
    #openid = session.get('openid') or 'ohCrcvlQMzCovAxALwzdNOs8yNf8'
    #openid = session.get('openid') or 'ohCrcvngXDgu-CulbG2S2IDXYbt4'
    agmstaffuser = Query(AgmStaffUser).equal_to('openid', openid).find()
    if not agmstaffuser and openid not in admins:
        return render_template('agm1.0/agm_wx_deny.html')
    if request.method == 'POST':
        if openid in admins:
            state = int(request.args.get('state'))
            rid = request.args.get('rid')
            report = Query(AgmStaffReport).get(rid)
            report.set('state', state+1)
            report.set('dealName', admins[openid]).save()
            return 'success'
    reports = Query(AgmStaffReport).equal_to('mode', 2).descending('createdAt').find()
    isadmin = openid in admins
    return render_template(
        'agmstaff/reports.html', 
        reports=reports, 
        int=int, 
        keyword=u"建议",
        isadmin=isadmin, 
        link="suggest", 
        printDateTime=printDateTime)

@app.route('/suggest', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def suggest():
    openid = session.get('openid')
    #openid = session.get('openid') or 'ohCrcvlQMzCovAxALwzdNOs8yNf8'
    agmstaffuser = Query(AgmStaffUser).equal_to('openid', openid).find()
    if not agmstaffuser:
        return render_template('agm1.0/agm_wx_deny.html')
    if request.method == 'POST':
        phone = request.form.get('phone')
        report_type = request.form.get('report_type')
        des = request.form.get('des')
        report = AgmStaffReport(phone=phone, des=des, report_type=report_type, mode=2, state=0)
        report.set('name', agmstaffuser[0].get('name'))
        report.set('depart', agmstaffuser[0].get('depart'))
        report.set('openid', openid)
        report.save()
        from tools.wechatAPI import send_report_message
        r_mode = [u'建议', 'suggests'] 
        for admin in admins_notify:
            send_report_message(admin, r_mode, report.get('name'), report.get('depart'), phone, report_type, des)
        r_code = "%s%s" % (printDateTime(report.created_at, '%Y%m%d%H%M%S'),
    report.id[-3:])
        r_code = [r_code, 'suggests']
        return render_template('agmstaff/msg.html', keyword=u"建议", r_code=r_code)
    name = agmstaffuser[0].get('name')
    return render_template('agmstaff/report.html', keyword=u"建议", name=name)

@app.route('/review', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def review():
    return render_template('agmstaff/review.html')

@app.route('/repair', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def repair():
    openid = session.get('openid')
    #openid = session.get('openid') or 'ohCrcvlQMzCovAxALwzdNOs8yNf8'
    agmstaffuser = Query(AgmStaffUser).equal_to('openid', openid).find()
    if not agmstaffuser and openid not in admins:
        return render_template('agm1.0/agm_wx_deny.html')
    if request.method == 'POST':
        if openid in fixer:
            state = int(request.args.get('state'))
            rid = request.args.get('rid')
            repair = Query(AgmStaffRepair).get(rid)
            repair.set('state', state+1)
            repair.set('repairName', fixer[openid]).save()
            return 'success'
    repairs = Query(AgmStaffRepair).descending('createdAt').find()
    isfixer = openid in fixer
    return render_template(
        'agmstaff/repair.html', 
        repairs=repairs, 
        places=places,
        floors=floors,
        int=int, 
        isfixer=isfixer, 
        printDateTime=printDateTime)
    
@app.route('/repair_submit', methods=["GET", "POST"])
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def repair_submit():
    openid = session.get('openid')
    #openid = session.get('openid') or 'ohCrcvlQMzCovAxALwzdNOs8yNf8'
    agmstaffuser = Query(AgmStaffUser).equal_to('openid', openid).find()
    if not agmstaffuser:
        return render_template('agm1.0/agm_wx_deny.html')
    if request.method == 'POST':
        place = int(request.form.get('place'))
        content = request.form.get('content')
        floor = int(request.form.get('floor'))
        des = request.form.get('des')
        agmstaffrepair = AgmStaffRepair(place=place, content=content, floor=floor, des=des, state=0)
        agmstaffrepair.set('openid', openid)
        agmstaffrepair.set('name', agmstaffuser[0].get('name'))
        agmstaffrepair.set('depart', agmstaffuser[0].get('depart'))
        agmstaffrepair.save()
        from tools.wechatAPI import send_repair_message
        place = '%s(%s)' % (places[place-1], floors[floor-1])
        for f in fixer_notify:
            send_repair_message(f, place, content, des)
        return render_template('agmstaff/msg.html', keyword=u"报修")
    return render_template('agmstaff/repair_submit.html')

@app.route('/rules')
def rules():
    return render_template('agmstaff/rules.html')

@app.route('/quetions')
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def quetions():
    return render_template('agmstaff/quetions.html')

@app.route('/flow')
@wrapsfuncs.wxauth_permission(['agmadmin', 'agmstaff'])
def flow():
    return render_template('agmstaff/flow.html')
